package com.pocketmenu.ui;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TableLayout;

import com.google.zxing.client.android.LocaleManager;
import com.pocketmenu.business.Constants;
import com.pocketmenu.business.RestaurantCursorAdapter;
import com.pocketmenu.persistence.Restaurant;
import com.pocketmenu.persistence.RestaurantDataSource;
import com.pocketmenu.persistence.SQLiteHelper;

public class RestaurantActivity extends Fragment{
	
	//public static List<Restaurant> restaurantList = new ArrayList<Restaurant>();	
	private SimpleCursorAdapter adapter;
	private Location currentLocation;
	
	private Activity getCurrentActivity()
	{
		return getActivity();
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (container == null) {            
            return null;
        }				                             
        
		View fragmentView = inflater.inflate(R.layout.activity_restaurant, container, false);
		
		//load item listview                        
        loadRestaurantListView(fragmentView);
        return fragmentView;
	}	
	
	private Cursor getItems()
    {
		RestaurantDataSource restaurantDatasource = new RestaurantDataSource(getCurrentActivity());
		restaurantDatasource.open();
				
		Cursor cursor = restaurantDatasource.getAllAsCursor();
			    
	    cursor.moveToFirst();
        restaurantDatasource.close();
	    return cursor;
    }
    
    private void loadRestaurantListView(View view)
    {
        ListView listView = (ListView) view.findViewById(R.id.lvRestaurants);
                               
        loadAdapter();
        listView.setAdapter(adapter);              
    }

    private void reloadRestaurantLisView()
    {
        ListView listView = (ListView) getView().findViewById(R.id.lvRestaurants);
                               
        loadAdapter();
        listView.setAdapter(adapter);              
    }
    
    private void loadAdapter()
    {
    	//Load adapter    	
    	String[] from = new String[]{SQLiteHelper.COLUMN_RESTAURANT_NAME, SQLiteHelper.COLUMN_RESTAURANT_DESCRIPTION};    	
      	int[] to = new int[]{R.id.tvRestaurantName,R.id.tvRestaurantDescription};
        
        Cursor cursor = getItems();    
        
        ListViewItemOnTouchListener touchListener = new ListViewItemOnTouchListener() {
			
			@Override
			public void onSwipe(View v, int action) {
				// TODO Auto-generated method stub				
			}
			
			@Override
			public void onClick(View v) {
				bListItems_Click(v);
				
			}
		};
        				
        adapter = new RestaurantCursorAdapter(getCurrentActivity(), R.layout.single_restaurant, cursor, from, to, touchListener);            
    }		
    
    private Location getCurrentLocation(){

    	LocationManager locationManager = (LocationManager) getCurrentActivity().getSystemService(Context.LOCATION_SERVICE);

		Criteria criteria = new Criteria();
    	criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
    	criteria.setAccuracy(Criteria.NO_REQUIREMENT);
    	String bestProvider = locationManager.getBestProvider(criteria, true);    			
    	
    	return locationManager.getLastKnownLocation(bestProvider);
    }
    
    private void bListItems_Click(View view)
    {
    	LinearLayout parent = (LinearLayout)view.getParent();
    	
    	ListView parentListview = (ListView) parent.getParent();
    	TabLayoutActivity tabLayout = TabLayoutActivity.getInstance();
    	tabLayout.restaurantSelectedIndex = parentListview.getPositionForView(parent);
    	
    	Constants.idRestaurant = ItemListActivity.restaurantList.get(tabLayout.restaurantSelectedIndex).getId();
    	    	
    	tabLayout.reloadCurrentTab = true;
    	tabLayout.onTabChanged("ItemListTab");
    	tabLayout.reloadCurrentTab = false;
    	 	
    }
}

